<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Service\IndexService;
use App\Service\Instance\JwtInstance;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Utils\Coroutine;

#[AutoController]
class IndexController extends AbstractController
{
    #[Inject]
    protected IndexService $indexService;
    public $a;

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

//    public function info() {
//        $id = (int)$this->request->input('id', 0); //..
//        if ($id > 0) {
//            return $this->response->success(['info' => 'data info:'.$id]);
//        } else {
//            return $this->response->fail(500, 'id无效');
//        }
//    }

    public function info()
    {
        $id = (int) $this->request->input('id', 0);
        $data = $this->indexService->inf($id);
        return $this->response->success($data);
    }

    public function memory() {
        return convert_size(memory_get_usage(true));
    }

    public function info2()
    {
        $a = $this->request->input('a');
        if ($a) {
            $this->a = $a;
        }
        return [
            'a' => $this->a,
            'co_is' => Coroutine::inCoroutine(),
            'co_id' => Coroutine::id(),
        ];
    }

    public function info3 ()
    {
        $id = $this->request->input('id');
        $user = [
            'id' => $id,
            'name' => $id.'_name'
        ];
        $instance = new JwtInstance();
        $instance->encode($user);
        return [
            'id' => $instance->id,
            'user' => $instance->user,
        ];
    }

    public function info4 () {
        $result = Db::select('SELECT * FROM email_code;');
        return $result;
    }

    public function sendmail()
    {
        return $this->response->success($this->indexService->sendMail());
    }

    public function sendmail2()
    {
        return $this->response->success($this->indexService->sendMailMq());
    }

}
